﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASPNETVN.PORTAL.Components.UI.DataTypes
{
    public class ListDataType : BaseDataType
    {
        private string _DataTextField;
        private string _DataValueField;

        public ListDataType()
        {
            this._InnerDataType = PropertyDataType.List;
        }

        public ListDataType(string csvList)
        {
            this._InnerDataType = PropertyDataType.List;
            this._InnerDataSource = csvList;
        }

        public override object DataSource
        {
            get
            {
                if (this._InnerDataSource != null)
                {
                    if (this._InnerDataSource is FileInfo[])
                    {
                        return this._InnerDataSource;
                    }
                    else
                    {
                        return this._InnerDataSource.ToString().Split(';');
                    }
                }
                else
                {
                    return null;
                }
            }
            set { this._InnerDataSource = value; }
        }

        public string DataValueField
        {
            get { return this._DataValueField; }
            set { this._DataValueField = value; }
        }

        public string DataTextField
        {
            get { return this._DataTextField; }
            set { this._DataTextField = value; }
        }

        public override Control EditControl
        {
            get
            {
                if (this._InnerControl == null)
                    InitializeComponents();

                // Update value in control.
                DropDownList dd = (DropDownList) this._InnerControl;
                dd.ClearSelection();
                if (dd.Items.FindByValue(Value) != null)
                {
                    dd.Items.FindByValue(Value).Selected = true;
                }

                // Return control.
                return this._InnerControl;
            }
            set
            {
                if (value.GetType().Name == "DropDownList")
                {
                    this._InnerControl = value;
                    // Update value from control.
                    DropDownList dd = (DropDownList) this._InnerControl;
                    if (dd.SelectedItem != null)
                    {
                        Value = dd.SelectedItem.Value;
                    }
                    else
                    {
                        Value = string.Empty;
                    }
                }
                else
                {
                    throw new ArgumentException("A DropDownList values is required");
                }
            }
        }

        protected override void InitializeComponents()
        {
            using (DropDownList dd = new DropDownList())
            {
                dd.Width = new Unit(this._ControlWidth);
                dd.DataSource = DataSource;
                dd.DataValueField = DataValueField;
                dd.DataTextField = DataTextField;
                dd.DataBind();
                this._InnerControl = dd;
            }
        }
    }
}